package Lab.Five.WallOfClutter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper 
{
	// Fields
	private static final String DATABASE_NAME = "WallOfClutter";
	private static final int DATABASE_VERSION = 1;
	private static final String TABLE_NAME = "Messages";
	private Context m_context;
	private SQLiteDatabase m_db;
	private SQLiteStatement m_insertStringStmt;
	private SQLiteStatement m_insertMessageStmt;
	private static final String INSERT_STRING = "insert into "
			+ TABLE_NAME + "(message) values (?)";
	private static final String INSERT_MESSAGE = "insert into "
			+ TABLE_NAME + "(id, message) values (?,?)";
	
	public DatabaseHelper(Context a_context)
	{
		m_context = a_context;
		OpenHelper openHelper = new OpenHelper(m_context);
		m_db = openHelper.getWritableDatabase();
		m_insertStringStmt = m_db.compileStatement(INSERT_STRING);
		m_insertMessageStmt = m_db.compileStatement(INSERT_MESSAGE);
	}
	
	public long insert(String message)
	{
		m_insertStringStmt.bindString(1, message);
		return m_insertStringStmt.executeInsert();
	}
	
	public long insertMessage(Message message)
	{
		String messageId = "" + message.getId();
		String messageText = message.getMessage();
		
		m_insertMessageStmt.bindString(1, messageId);
		m_insertMessageStmt.bindString(2, messageText);
		return m_insertMessageStmt.executeInsert();
	}
	
	public void deleteAll()
	{
		m_db.delete(TABLE_NAME, null, null);
	}
	
	public int getNumOfMessages()
	{
		return countMessages();
	}
	
	private int countMessages()
	{
		List<String> list = new ArrayList<String>();
		Cursor cursor = m_db.query(TABLE_NAME, new String[] { "id" }, null, null, null, null, "id ASC");
		
		if (cursor.moveToFirst())
		{
			do 
			{
			list.add(cursor.getString(0));
			}
			while(cursor.moveToNext());
		}
		if ( cursor != null & !cursor.isClosed())
		{
			cursor.close();
		}
		
		return list.size();
	}
	
	public List<Message> selectAll()
	{
		List<Message> list = new ArrayList<Message>();
		Cursor cursor = m_db.query(TABLE_NAME, new String[] { "id", "message" }, 
				null, null, null, null, "message ASC");
		if (cursor.moveToFirst())
		{
			do 
			{
				list.add(new Message(Integer.valueOf(cursor.getString(0)), cursor.getString(1)));
			}
			while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed())
		{
			cursor.close();
		}
		return list;
	}	
	
	private static class OpenHelper extends SQLiteOpenHelper
	{
		OpenHelper(Context context)
		{
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) 
		{
			db.execSQL("CREATE TABLE " + TABLE_NAME + 
			"(id INTEGER PRIMARY KEY, message TEXT)");
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
		{
			ErrorHandling.newDebugEvent("WARNING: Upgrading database, this will drop tables.");
			db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
			onCreate(db);
		}
	}
}
